<template>
  <div>
    <el-card header="签名证书管理">
      <el-form :inline="true" :model="queryParam">
        <el-form-item label="有效期">
          <el-date-picker
            v-model="queryParam.VaildTime"
            type="date"
            placeholder="选择有效日期"
          />
        </el-form-item>
        <el-form-item label="证书创建方式">
          <el-select v-model="queryParam.IsEmpUp" placeholder="选择证书创建方式">
            <el-option label="全部" :value="null" />
            <el-option label="用户上传" :value="true" />
            <el-option label="系统创建" :value="false" />
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="success" @click="add">新建证书</el-button>
          <el-button type="primary" @click="upSign">上传已有证书</el-button>
          <el-button @click="reset">重置</el-button>
        </el-form-item>
      </el-form>
      <w-table
        :data="certList"
        :columns="columns"
        :is-paging="false"
        row-key="Id"
      >
        <template slot="dept" slot-scope="e">
          {{
            e.row.Country + "," + e.row.Company + "," + e.row.Dept
          }}
        </template>
        <template slot="encrypType" slot-scope="e">
          <span v-if="e.row.EncryptionType == 0">未知</span>
          <span v-else-if="e.row.EncryptionType == 1">【国标算法】2048位RSA</span>
          <span v-else-if="e.row.EncryptionType == 2">【国标算法】3072位RSA</span>
          <span v-else-if="e.row.EncryptionType == 3">【国密算法】SM2</span>
        </template>
        <template slot="isEmpUp" slot-scope="e">
          {{ e.row.IsEmpUp ? "用户上传" : "系统创建" }}
        </template>
        <template slot="action" slot-scope="e">
          <el-button size="mini" type="danger" icon="el-icon-delete" circle @click="dropSign(e.row)" />
        </template>
        <template slot="time" slot-scope="e">
          {{
            moment(e.row.BeginTime).format("YYYY-MM-DD HH:mm:ss") +
              " 至 " +
              moment(e.row.EndTime).format("YYYY-MM-DD HH:mm:ss")
          }}
        </template>
      </w-table>
    </el-card>
    <empSignAdd :visible.sync="visible" @close="close" />
    <empSignUp :visible.sync="upVisible" @close="close" />
  </div>
</template>

<script>
import moment from 'moment'
import * as empSignApi from '@/api/emp/empSign.js'
import empSignAdd from './components/empSignAdd.vue'
import empSignUp from './components/empSignUp.vue'
export default {
  components: {
    empSignAdd,
    empSignUp
  },
  props: {
    isLoad: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      certList: [],
      visible: false,
      upVisible: false,
      columns: [
        {
          key: 'SignName',
          title: '证书名称',
          align: 'center'
        },
        {
          key: 'Email',
          title: '邮箱',
          align: 'center'
        },
        {
          key: 'dept',
          title: '国家单位部门',
          align: 'center',
          slotName: 'dept'
        },
        {
          key: 'time',
          title: '有效期',
          align: 'center',
          slotName: 'time'
        },
        {
          key: 'encrypType',
          title: '加密方式',
          align: 'center',
          slotName: 'encrypType'
        },
        {
          key: 'IsEmpUp',
          title: '证书创建方式',
          align: 'center',
          slotName: 'isEmpUp'
        },
        {
          key: 'action',
          title: '操作',
          align: 'left',
          width: '100px',
          slotName: 'action'
        }
      ],
      paging: {
        Size: 20,
        Index: 1,
        SortName: 'Id',
        IsDesc: false,
        Total: 0
      },
      queryParam: {
        IsEmpUp: null,
        VaildTime: null
      },
      isload: false
    }
  },
  computed: {
    comId() {
      return this.$store.getters.curComId
    }
  },
  watch: {
    isLoad: {
      handler(val) {
        if (val) {
          this.reset()
        }
      },
      immediate: true
    }
  },
  methods: {
    moment,
    close(isRefresh) {
      if (isRefresh) {
        this.loadGrid()
      }
    },
    upSign() {
      this.upVisible = true
    },
    add() {
      this.visible = true
    },
    async loadGrid() {
      const res = await empSignApi.Query(this.queryParam, this.paging)
      if (res.Count === 0) {
        this.certList = []
      } else {
        this.certList = res.List
      }
      this.paging.Total = res.Count
    },
    dropSign(row) {
      const title = '确认删除签名文件 ' + row.SignName + '?'
      const that = this
      this.$confirm(title, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        that.submitDrop(row.Id)
      })
    },
    async submitDrop(id) {
      await empSignApi.Delete(id)
      this.$message({
        type: 'success',
        message: '删除成功!'
      })
      this.loadGrid()
    },
    reset() {
      this.queryParam.VaildTime = null
      this.queryParam.IsEmpUp = null
      this.paging.Index = 1
      this.loadGrid()
    }
  }
}
</script>
